package com.luoyuanxiangvip.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luoyuanxiangvip.common.utils.AjaxResult;
import com.luoyuanxiangvip.common.utils.PageUtil;
import com.luoyuanxiangvip.common.vo.BizCommentVO;
import com.luoyuanxiangvip.core.entity.BizComment;
import com.luoyuanxiangvip.core.service.IBizCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019-05-26
 */
@RestController
@RequestMapping("/admin/comment")
@RequiresAuthentication
@Api(description = "评论管理")
public class BizCommentController{

    @Resource
    private IBizCommentService iBizCommentService;

    @ApiOperation(value = "获取评论列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody BizCommentVO bizCommentVO) {
        IPage<BizComment> page = iBizCommentService.findPageBreakByCondition(bizCommentVO);
        return AjaxResult.success("获取评论列表成功", new PageUtil<>(page));
    }

    @ApiOperation(value = "修改状态")
    @PatchMapping("/updateStatus/{id}")
    public AjaxResult saveOrUpdate(@PathVariable("id") Long id) {
        BizComment byId = iBizCommentService.getById(id);
        byId.setStatus(!byId.getStatus());
        return iBizCommentService.updateById(byId) ? AjaxResult.success("修改状态成功") : AjaxResult.error("修改状态失败");
    }

    @ApiOperation(value = "获取评论详情")
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        return AjaxResult.success("获取评论详情成功", iBizCommentService.getById(id));
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        return iBizCommentService.removeById(id) ? AjaxResult.success("删除评论成功") : AjaxResult.error("删除评论失败");
    }

    @ApiOperation(value = "评论条数查询")
    @GetMapping("/commentCount")
    public AjaxResult commentCount() {
        int count = iBizCommentService.count();
        return AjaxResult.success("评论条数查询成功", count);
    }

}
